import { Service } from '../../services/service'
import { formaturl } from '../../utils/commonUtils'
const state = {
  detailInfo: {},
  detailType: 1,
  reqUrlsMapping: {
    1: '/hanyun/app-service/api/v1/open/app/info',
    2: '/hanyun/app-service/api/v1/open/service/info',
    3: '/hanyun/app-service/api/v1/open/model/info'
  },
  subscribeUrlsMapping: {
    1: 'hanyun/app-service/api/v1/tbShopAppSubscribe/add',
    2: 'hanyun/app-service/api/v1/tbShopServiceSubscribe/add',
    3: 'hanyun/app-service/api/v1/tbShopModelSubscribe/add'
  },
  unSubscribeUrlsMapping: {
    1: 'hanyun/app-service/api/v1/tbShopAppSubscribe/delete',
    2: 'hanyun/app-service/api/v1/tbShopServiceSubscribe/delete',
    3: 'hanyun/app-service/api/v1/tbShopModelSubscribe/delete'
  },
  subscribeIdMapping: {
    1: 'shopAppId',
    2: 'shopServiceId',
    3: 'shopModelId'
  },
  userInfo: {}
}
const getters = {}
const mutations = {
  setDetailInfo(state, info) {
    state.detailInfo = info
  },
  setDetailType(state, type) {
    state.detailType = type
    sessionStorage.setItem('pageType', type)
  },
  setUserInfo(state, info) {
    state.userInfo = info
  }
}
const actions = {
  async getDetails({ state, commit }, params) {
    const ret = await Service({
      url: state.reqUrlsMapping[params.currentTab] + formaturl(params.params),
      method: 'get'
    })
    if (ret.status === 200) {
      commit('setDetailInfo', ret.result)
    }
  },
  async subscribeApp({ state }, params) {
    const ret = await Service({
      url: state.subscribeUrlsMapping[params.currentTab],
      method: 'post',
      data: {
        [state.subscribeIdMapping[params.currentTab]]: params.params.id
      }
    })
    return new Promise((resolve, reject) => {
      if (ret.status === 200) {
        resolve('订阅成功!')
      } else {
        reject(ret.msg)
      }
    })
  },
  async unSubscribeApp(_, params) {
    const ret = await Service({
      url:
        state.unSubscribeUrlsMapping[params.currentTab] +
        formaturl({
          shopId: params.params.id,
          ...params.params
        }),
      method: 'post',
      data: {}
    })
    return new Promise((resolve, reject) => {
      if (ret.status === 200) {
        resolve('取消订阅成功!')
      } else {
        reject(ret.msg)
      }
    })
  },
  async getUserInfo ({ state, commit }, param) {
    const res = await Service({
      url: 'hanyun/user-service/api/v1/user/findSysUserById',
      method: 'get',
      params: param
    })
    if (res && res.status === 200) {
      commit('setUserInfo', res.result)
    }
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
